<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--user main page-->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Content-Language" content="zh-CN">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Url shorten app</title>

    <!--    import jquery, bootstrap and datatable source by cdn-->
    <script th:src="@{https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js}"></script>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.15/css/jquery.dataTables.css">``
    <script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.15/js/jquery.dataTables.js"></script>

    <style>
        @media (min-width: 1200px) {
            .container {
                width: 751px;
            }
        }
    </style>
</head>
<body>

<div class="modal fade" id="stat_model" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&nbsp;&times;</span>
                </button>
                <h4 class="modal-title">Urls statistic</h4>
            </div>
            <div class="modal-body">

                <table id="url_table" class="table table-bordered no-footer dataTable">
                    <thead>
                    <tr>
                        <th>Original Url</th>
                        <th>Shorten Url</th>
                        <th>Visits</th>
                        <th>Last visited time</th>
                    </tr>
                    </thead>
                    <tbody th:each="url:${myUrl}">
                    <tr>
                        <td th:text="${url.originUrl}"></td>
                        <td th:text="${url.shortUrlId}"> </td>
                        <td th:text="${url.visits}"></td>
                        <td th:text="${url.lastVisited}"></td>
                    </tr>
                    </tbody>
                </table>

            </div>
            <div class="modal-footer">
                <a id="cls_model" href="javascript:void(0)" class="btn btn-primary" data-dismiss="modal">Close</a>
            </div>
        </div>
    </div>
</div>


<div class="container">
    <br>
    <h1 style="font-size: x-large;" align="center">Url shorten app</h1><br>
    <br>
    <div class="row" th:if="${session.user}">
        <div class="col-xs-12 col-md-12" align="right">
            <button id="statBtn" class="btn btn-primary" data-toggle="modal" data-target="#stat_model" >My Urls</button>
            <a class="btn btn-info" href="/logout">Logout</a>
        </div>
    </div>
    <br>
    <form id="url">
        <div class="row">
            <div class="col-xs-12 col-md-12">
                    <textarea rows="5" class="form-control" placeholder="Support batch generate shorten Url, one line one long Url" id="longUrl"
                              name="longUrl"></textarea>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-xs-12 col-md-12" align="center">
                <button id="genBtn" type="button" class="btn btn-success">generate</button>
                <button type="reset" class="btn btn-warning">reset</button>
            </div>
        </div>
    </form>


    <input th:value="${session.separate}" type="hidden" id="separate"/>

    <span id="shortUrl" hidden="hidden" style="font-size: large;color: red"></span>
</div>

<script>

    $(function () {



        // longUrl
        $("#genBtn").click(function () {

            if ($("#longUrl").val().length <= 0){
                alert("链接不能为空");
            }else{
                $.ajax({
                    url: "/createUrl",
                    method: "POST",
                    data: {"longUrl": $("#longUrl").val()},
                    success: function (res) {
                        //{"urlRes":{"data":"http://localhost/a3B48/5Xe0Z4\n","flag":true,"msg":"CREATE_SUC"}}

                        res = JSON.parse(res);

                        if (res.urlRes.flag){
                            let urls = res.urlRes.data;
                            $("#shortUrl").html(urls);
                            $("#shortUrl").show();
                        }
                    }
                })
            }

        })

        $("#statBtn").click(function () {

            $("#url_table").dataTable();

            $("#stat_modal").modal();
        })


        //销毁datatable
        $("#cls_modal").click(function () {
            $("#url_table>tbody").empty();
            if ($('#url_table').hasClass('dataTable')) {
                let dttable = $('#url_table').dataTable();
                dttable.fnClearTable();
                dttable.fnDestroy();
            }
        })
    })
</script>
</body>
</html>